package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class TestServiceImpl implements TestService {


    @Autowired
    private RedisTemplate redisTemplate;



    /**
     * 写入redis测试
     */
    @Override
    public  void setRedis() {
        //如果key存在,set操作失败;只要有一个线程进入,其他线程则不能进行操作;相当于加锁了
        Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", "123");
        if (lock ){
            Integer atguigu = (Integer)redisTemplate.opsForValue().get("atguigu");
            if (atguigu != null){
                atguigu++;
                redisTemplate.opsForValue().set("atguigu",atguigu);
            }
            //删除lock,这样一个线程操作完,下一个线程才能进入
            redisTemplate.delete("lock");
        }
        //递归
        setRedis();
    }

    /**
     * 写入redisson测试
     */
    @Override
    public void setRedisson() {

    }
}
